﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using MongoDb.EntityFramework;
using MongoDB.Bson;
using MongoDB.Driver;

namespace SManagement.Controllers
{
    public abstract class TypeDesignationGenericController<T> : Controller
        where T : Models.TypeDesignation, new()
    {
        private Type type = typeof(T);

        // GET: AdministrativeFundingControl
        public ActionResult Index()
        {
            var classObject = MongoDbContext.Current.All<T>();

            return View(classObject);
        }

        // GET: AdministrativeFundingControl/Details/5
        public ActionResult Details(string id)
        {
            T classObject = MongoDbContext.Current.Single<T>(i => i._id == ObjectId.Parse(id));

            return View(classObject);
        }

        // GET: AdministrativeFundingControl/Create
        public ActionResult Create()
        {
            return View("CreateEdit", (T)Activator.CreateInstance(typeof(T)));
        }

        // POST: AdministrativeFundingControl/Create
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "_id")] T classObject)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    InsertUpdateDB(classObject);

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View("CreateEdit", classObject);
                }
            }
            else
            {
                return View("CreateEdit", classObject);
            }
        }

        // GET: AdministrativeFundingControl/Edit/5
        public ActionResult Edit(string id)
        {
            var classObject = MongoDbContext.Current.Single<T>(s => s._id == ObjectId.Parse(id));

            return View("CreateEdit", classObject);
        }

        // POST: AdministrativeFundingControl/Edit/5
        [HttpPost]
        public ActionResult Edit(string id, T classObject)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    InsertUpdateDB(classObject);

                    return RedirectToAction("Index");
                }
                catch
                {
                    return View("CreateEdit", classObject);
                }
            }
            else
            {
                return View("CreateEdit", classObject);
            }
        }

        // GET: AdministrativeFundingControl/Delete/5
        public ActionResult Delete(string id)
        {
            DeleteFromDB(id);
            return RedirectToAction("Index");
        }

        private void InsertUpdateDB(T obj)
        {
            if (obj._id == null || obj._id == ObjectId.Empty)
            {
                MongoDbContext.Current.Add(obj);
            }
            else
            {
                MongoDbContext.Current.Update(obj, o => o._id == obj._id);
            }
        }

        private void DeleteFromDB(string id)
        {
            MongoDbContext.Current.Delete<T>(i => i._id == ObjectId.Parse(id));
        }
    }
}